//
//  LoginViewController.swift
//  clreader
//
//  Created by colin on 2019/2/26.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit
import Then

class LoginViewController: CLBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        // logo
        let imageViewLogo = UIImageView(image: UIImage(named: "logo")).then {
            $0.layer.cornerRadius = 20.0
            $0.layer.masksToBounds = true
        }
        self.view.addSubview(imageViewLogo)
        imageViewLogo.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 92.0, height: 92.0))
            make.centerX.equalToSuperview()
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(50.0)
        }
        // 阴影背景
        let imageViewLogoShadow = UIView().then {
            $0.backgroundColor = UIColor.white
            $0.layer.cornerRadius = 20.0
            $0.layer.shadowColor = UIColor.black.cgColor
            $0.layer.shadowOffset = CGSize(width:2.0,height:4.0)
            $0.layer.shadowOpacity = 0.3 //阴影
        }
        self.view.insertSubview(imageViewLogoShadow, belowSubview: imageViewLogo)
        imageViewLogoShadow.snp.makeConstraints({ (make) in
            make.edges.equalTo(imageViewLogo)
        })
        
        // 登录按钮
        let buttonLogin = UIButton(type: .custom).then {
            $0.backgroundColor = UIColor(hex: "AE1600")
            $0.layer.cornerRadius = 24.5
            $0.layer.masksToBounds = true
            $0.setTitle("账号登录", for: .normal)
            $0.setTitleColor(UIColor.white, for: .normal)
            $0.titleLabel?.font = UIFont.systemFont(ofSize: 17.0, weight:.bold)
            $0.addTarget(self, action: #selector(self.actionLogin(_:)), for: .touchUpInside)
        }
        self.view.addSubview(buttonLogin)
        buttonLogin.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20.0)
            make.right.equalToSuperview().offset(-20.0)
            make.top.equalTo(imageViewLogo.snp.bottom).offset(80.0)
            make.height.equalTo(49.0)
        }
        
        // 注册按钮
        let buttonRegister = UIButton(type: .custom).then {
            $0.backgroundColor = UIColor.white
            $0.layer.cornerRadius = 24.5
            $0.layer.masksToBounds = true
            $0.layer.borderColor = UIColor(hex: "AE1600").cgColor
            $0.layer.borderWidth = 1.0
            $0.setTitle("注册", for: .normal)
            $0.setTitleColor(UIColor(hex: "AE1600"), for: .normal)
            $0.titleLabel?.font = UIFont.systemFont(ofSize: 17.0, weight:.bold)
            $0.addTarget(self, action: #selector(self.actionRegister(_:)), for: .touchUpInside)
        }
        self.view.addSubview(buttonRegister)
        buttonRegister.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20.0)
            make.right.equalToSuperview().offset(-20.0)
            make.top.equalTo(buttonLogin.snp.bottom).offset(20.0)
            make.height.equalTo(49.0)
        }
        
        // 随便看看
        let buttonJump = UIButton(type: .custom).then {
            $0.setTitle("跳过注册，随便看看", for: .normal)
            $0.setTitleColor(UIColor(hex: "828282"), for: .normal)
            $0.titleLabel?.font = UIFont.systemFont(ofSize: 12.0, weight:.light)
            $0.addTarget(self, action: #selector(self.actionJump(_:)), for: .touchUpInside)
        }
        self.view.addSubview(buttonJump)
        buttonJump.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(buttonRegister.snp.bottom).offset(30.0)
        }
        
        // 其他登录方式
        let isWechatInstalled = true
        let isQQInstalled = true
        if isWechatInstalled || isQQInstalled {
            
            let container = UIView().then {
                
                if isWechatInstalled {
                    let item = UIView().then {
                        let buttonWechat = UIButton(type: .custom).then {
                            $0.setImage(UIImage(named: "icon_login_wechat"), for: .normal)
                            $0.addTarget(self, action: #selector(self.actionWechatLogin(_:)), for: .touchUpInside)
                        }
                        $0.addSubview(buttonWechat)
                        buttonWechat.snp.makeConstraints({ (make) in
                            make.size.equalTo(CGSize(width: 40.0, height: 40.0))
                            make.centerX.equalToSuperview()
                            make.top.equalToSuperview()
                        })
                        
                        let labelWechat = UILabel().then {
                            $0.text = "微信登录"
                            $0.textColor = UIColor(hex: "BDBDBD")
                            $0.font = UIFont.systemFont(ofSize: 11.0, weight:.light)
                        }
                        $0.addSubview(labelWechat)
                        labelWechat.snp.makeConstraints({ (make) in
                            make.centerX.equalToSuperview()
                            make.top.equalTo(buttonWechat.snp.bottom).offset(13.0)
                            make.bottom.equalToSuperview()
                        })
                    }
                    
                   $0.addSubview(item)
                    item.snp.makeConstraints({ (make) in
                        make.left.equalToSuperview()
                        make.top.equalToSuperview()
                        make.bottom.equalToSuperview()
                        make.width.equalTo(50.0)
                        if !isQQInstalled {
                            make.right.equalToSuperview()
                        }
                    })
                }
                
                if isQQInstalled {
                    let item = UIView().then {
                        let buttonQQ = UIButton(type: .custom).then {
                            $0.setImage(UIImage(named: "icon_login_qq"), for: .normal)
                            $0.addTarget(self, action: #selector(self.actionQQLogin(_:)), for: .touchUpInside)
                        }
                        $0.addSubview(buttonQQ)
                        buttonQQ.snp.makeConstraints({ (make) in
                            make.size.equalTo(CGSize(width: 40.0, height: 40.0))
                            make.centerX.equalToSuperview()
                            make.top.equalToSuperview()
                        })
                        
                        let labelQQ = UILabel().then {
                            $0.text = "QQ登录"
                            $0.textColor = UIColor(hex: "BDBDBD")
                            $0.font = UIFont.systemFont(ofSize: 11.0, weight:.light)
                        }
                        $0.addSubview(labelQQ)
                        labelQQ.snp.makeConstraints({ (make) in
                            make.centerX.equalToSuperview()
                            make.top.equalTo(buttonQQ.snp.bottom).offset(13.0)
                            make.bottom.equalToSuperview()
                        })
                    }
                    
                    $0.addSubview(item)
                    item.snp.makeConstraints({ (make) in
                        make.right.equalToSuperview()
                        make.top.equalToSuperview()
                        make.width.equalTo(50.0)
                        if !isWechatInstalled {
                            make.left.equalToSuperview()
                        } else {
                            make.left.equalToSuperview().offset(50.0 + 50.0)
                        }
                    })
                }
                
            }
            
            self.view.addSubview(container)
            container.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-20.0)
            }
            
            let labelOther = UILabel().then {
                $0.textColor = UIColor(hex: "BDBDBD")
                $0.text = "其他登录方式"
                $0.font = UIFont.systemFont(ofSize: 13.0, weight:.light)
            }
            self.view.addSubview(labelOther)
            labelOther.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.bottom.equalTo(container.snp.top).offset(-25.0)
            }
            
            let lineLeft = UIImageView(image: UIImage(named: "line_login_left"))
            self.view.addSubview(lineLeft)
            lineLeft.snp.makeConstraints { (make) in
                make.left.equalToSuperview()
                make.right.equalTo(labelOther.snp.left).offset(-10.0)
                make.centerY.equalTo(labelOther.snp.centerY)
                make.height.equalTo(0.5)
            }
            
            let lineRight = UIImageView(image: UIImage(named: "line_login_right"))
            self.view.addSubview(lineRight)
            lineRight.snp.makeConstraints { (make) in
                make.right.equalToSuperview()
                make.left.equalTo(labelOther.snp.right).offset(10.0)
                make.centerY.equalTo(labelOther.snp.centerY)
                make.height.equalTo(0.5)
            }
            
        }

    }

}

extension LoginViewController {
    /// 随便看看
    @objc private func actionJump(_ sender:Any) {
        self.navigationController?.dismiss(animated: true, completion: nil)
    }
    
    /// 账号登录
    @objc private func actionLogin(_ sender:Any) {
        let controller = PasswordLoginViewController()
        self.show(controller, sender: nil)
    }
    
    /// 注册
    @objc private func actionRegister(_ sender:Any) {
        let controller = PhoneViewController()
        self.show(controller, sender: nil)
    }
    
    /// 微信登录
    @objc private func actionWechatLogin(_ sender:Any) {
        print("微信登录")
    }
    
    /// qq登录
    @objc private func actionQQLogin(_ sender:Any) {
        print("qq登录")
    }
}
